<template>
  <div class="mod-config">
    <el-button type="primary" @click="uploadPic();">添加硬笔系统</el-button>
    <el-dialog title="添加硬笔系统" :visible.sync="dialogFormVisible" :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="120px" class="demo-ruleForm">
        <el-form-item label="系统名称："
          prop="name"
          :rules="[{ required: true, message: '系统名称不能为空'}]">
          <el-input
              type="name"
              placeholder="请输入内容"
              v-model="form.name"
              style="width: 50%"
              clearable>
            </el-input>
        </el-form-item>
        <el-form-item label="系统顺序："
          prop="sorder"
          :rules="[{ required: true, message: '硬笔书顺序不能为空'}]">
          <el-input-number v-model="form.sorder" controls-position="right" :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="硬笔系统封面：" prop="imageUrl" :rules="[{ required: true, message: '硬笔系统封面未上传'}]">
          <el-upload 
          class="avatar-uploader" 
          :action="'http://121.41.167.195:8000/api/oss/upload'"
          :on-success="handleAvatarSuccess"
          :auto-upload="true"
          :show-file-list="false">
            <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="insertSystem();">保 存</el-button>
      </div>
    </el-dialog>
    <el-table
      :data="dataList"
      stripe
      v-loading="dataListLoading"
      style="width: 100%"
    >
      <el-table-column prop="id" label="教学系统id" width="180">
      </el-table-column>
      <el-table-column prop="systemName" label="系统名称" width="180">
      </el-table-column>
      <el-table-column prop="name" label="系统封面">
          <template slot-scope="scope">
              <el-image :src="scope.row.systemCover"></el-image>
        </template> </el-table-column>
      <el-table-column prop="sorder" label="系统顺序"> </el-table-column>
      <el-table-column prop="createtime" label="创建时间"> </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="updateSystem(scope.row.id,scope.row.systemName,scope.row.systemCover,scope.row.sorder)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>

  </div>
</template>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<script>
export default {
  data() {
    return {
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dialogVisible: false,
      dialogFormVisible: false,
      form: {
          name: '',
          imageUrl: '',
          updateId: '',
          sorder: 0,
        }
    };
  },
  components: {},
  activated() {
    this.getDataList();
  },
  methods: {
    //上传按钮事件
    uploadPic() {
      this.dialogFormVisible = true;
    },
    //上传成功后的回调函数
    handleAvatarSuccess(response) {
      this.form.imageUrl = response.url;
    },
    //存硬笔系统记录
    insertSystem(){
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if(this.form.updateId == ''){
            this.$http({
              url: this.$http.adornUrl("/sf/cbstiffsystem/save"),
              method: "post",
              data: this.$http.adornParams({
                systemName: this.form.name,
                systemCover: this.form.imageUrl,
                sorder: this.form.sorder,
              }),
            }).then(({ data }) => {
              this.form.imageUrl = '';
              this.form.name = '';
              this.form.sorder = 0;
              this.dialogFormVisible = false;
              this.$message({
                message: '添加成功!',
                type: 'success'
              });
              this.getDataList();
            });
          }else{
            this.$http({
              url: this.$http.adornUrl("/sf/cbstiffsystem/update"),
              method: "post",
              data: this.$http.adornParams({
                id: this.form.updateId,
                systemName: this.form.name,
                systemCover: this.form.imageUrl,
                sorder: this.form.sorder,
              }),
            }).then(({ data }) => {
              this.form.imageUrl = '';
              this.form.name = '';
              this.form.sorder = 0;
              this.dialogFormVisible = false;
              this.$message({
                message: '修改成功!',
                type: 'success'
              });
              this.getDataList();
            });
          }
        }else{
          this.$message.error('系统名称不能为空或系统封面未上传！');
        }
      });
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.form.imageUrl = '';
          this.form.name = '';
          this.form.updateId = '';
          done();
        })
        .catch(_ => {});
    },
    // 获取教学系统分页数据
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/sf/cbstiffsystem/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.stiffSystemPage.records;
          this.totalPage = data.stiffSystemPage.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 新增或修改
    updateSystem(systemId,systemName,systemCover,sorder) {
      this.dialogFormVisible = true;
      this.form.name = systemName;
      this.form.imageUrl = systemCover;
      this.form.updateId = systemId;
      this.form.sorder = sorder;
    },
    // 删除
    deleteHandle(systemId) {
      this.$confirm(`确定对[硬笔系统id=${systemId}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
          console.log([systemId])
          this.$http({
            url: this.$http.adornUrl("/sf/cbstiffsystem/delete"),
            method: "post",
            data: this.$http.adornData([systemId], false),
          }).then(({ data }) => {
            console.log(data);
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>